package com.jt.controller;

import com.jt.service.FileService;
import com.jt.vo.ImageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RestController
public class FileController {
    @Autowired
    private FileService fileService;

    @RequestMapping("/file")
    public String file(MultipartFile fileImage) throws IOException {
        //获取图片名称
        String name = fileImage.getOriginalFilename();
        //准备文件上传目录
        String dir="D:/JT-SOFT/images";
        //利用对象封装路径
        File dirFile = new File(dir);//文件目录对象
        if(!dirFile.exists()){//不存在则创建目录
            dirFile.mkdirs();
        }
        //实现文件上传
        File file = new File(dir + "/" + name);//文件全路径对象
        fileImage.transferTo(file);
        return "操作成功!!!";
    }





    //实现文件上传
    @RequestMapping("/pic/upload")
    public ImageVo upload(MultipartFile uploadFile){
        return fileService.upload(uploadFile);
    }
}
